package com.my.chapter15;

import java.util.HashMap;
import java.util.Map;

public class practice24 {
    public static void main(String[] args) {
        FactoryCapture fc = new FactoryCapture();
        fc.addFactory("Integer",new IntegerFactory());
        System.out.println(fc.createNew("Integer",6));
        fc.createNew("Product",6);
    }
}

class FactoryCapture {
    Map<String,FactoryI<?>> factories =
            new HashMap<String,FactoryI<?>>();
    public Object createNew(String factoryname, int arg) {
        FactoryI<?> f = factories.get(factoryname);
        try {
            return f.create(arg);
        }catch (NullPointerException e){
            System.out.println("Not a registered factoryname: " + factoryname);
        }
        return null;
    }
    public void
    addFactory(String factoryname, FactoryI<?> factory) {
        factories.put(factoryname, factory);
    }
}
